import BaseModel from './BaseModel'
import { get, post } from '../api/ajax'
import API from '../api/apis'
import { ImageMfit, ImageFill, PixelUnitType } from '../utils/filters'

// 首页轮播图对象
class BannerModel extends BaseModel {

  // 图片链接
  public pic = ''
  // 标题
  public title = ''
  // 详情
  public detil = ''
  // 跳转链接
  public switch = ''
  // 索引
  public index = 0

  // 构造方法
  constructor() {
    super()
  }
  /* ----- 获取指定图片大小 -------*/
  // 手机端首页图片
  get mHomeBannerUrl() {
    return ImageMfit(this.pic, 7.5, 7.5, PixelUnitType.rem)
  }

  // PC端首页图片
  get pcHomeBannerUrl() {
    return ImageFill(this.pic, 600, 400)
  }

  // 删除对象
  public removeBanner() {
    return new Promise((resolve, reject) => {
      get(API.banner.remove, {
        bannerId: this._id,
      }).then((data) => {
        resolve(data)
      }).catch((err) => {
        reject(err)
      })
    })
  }

  // 添加或者修改banner
  public addOrEddBanner(isEdd: boolean) {
    const url = isEdd ? API.banner.update : API.banner.add
    return new Promise((resolve, reject) => {
      post(url, this.getJson(['title', 'detil', 'index', 'switch', 'pic'], {
        bannerId: this._id,
      })).then(() => {
        resolve()
      }).catch((err) => {
        reject(err)
      })
    })
  }
}

export default BannerModel
